using System;
using Anarchy.DB;
using Anarchy.FS;

namespace Anarchy
{

    public enum ResultType
    {
        Image,
        Thumbnail,
        ImageThumbnail,
        Details
    }

	public class AnarchyMain
	{
	
		// Variables
		DBWrapper DB;
		FSWrapper FS;
		
		// Constructor
		public AnarchyMain(DBWrapper _DB, FSWrapper _FS)
		{
			
			// Variables
			DB = _DB;
			FS = _FS;

        }

        #region Actions

        // Add Image
        public void Add(ImageInfo _Img)
        {

            // .. To DB
            DB.Add(_Img);

            // .. To FS
            FS.Add(_Img);

        }

        // Delete Image
        public void Delete(ImageInfo _Img)
        {

            // .. From DB
            DB.Delete(_Img);

            // .. From FS
            FS.Delete(_Img);

        }
        
        // Update Image
        public void Image(ImageInfo _Img)
        {
            throw new NotImplementedException("The 'update' function is not implemented.");
        }

        // GetList
        public DirectoryInfo GetList(String BaseDir)
        {

            // ..
            return FS.GetList(BaseDir);

        }

        // Get
        public ImageInfo Get(ref ImageInfo _Img)
        {
            
            // .. From DB
            ImageInfo __Img = DB.Select(ref _Img);

            return __Img;

        }

        // Select [By ID]
        public ImageInfo Select(int _ID, ResultType _Type)
        {

            // .. From DB
            ImageInfo _Img = DB.Select(_ID);

            // .. Stuff from file-system.. Maybe
            if ((_Type == ResultType.Image) || (_Type == ResultType.ImageThumbnail))
                _Img.Stream = FS.GetStream(_Img);
            if ((_Type == ResultType.Thumbnail) || (_Type == ResultType.ImageThumbnail))
                _Img.ThumbStream = FS.GetThumbStream(_Img);

            // .. Return
            return _Img;

        }

        // Select [By Tag]
        public ImageInfo[] Select(String[] _Tags, ResultType _Type)
        {

            // .. From DB
            ImageInfo[] _Imgs = DB.Select(_Tags);

            // .. Stuff from file-system.. maybe
            foreach (ImageInfo _Img in _Imgs)
            {

                // Get the file stuff.
                if ((_Type == ResultType.Image) || (_Type == ResultType.ImageThumbnail))
                    _Img.Stream = FS.GetStream(_Img);
                if ((_Type == ResultType.Thumbnail) || (_Type == ResultType.ImageThumbnail))
                    _Img.ThumbStream = FS.GetThumbStream(_Img);

            }

            // .. Return
            return _Imgs;

        }

        #endregion




    }
}
